package com.meessiom.train.crm.RepaymentSchedule.model;

import com.meessiom.train.crm.utils.RDU;
import lombok.*;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "repayment_schedule_info")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RepaymentScheduleInfo {
    // 状态常量
    public static final String STATUS_PENDING = "PENDING";
    public static final String STATUS_PAID = "PAID";
    public static final String STATUS_OVERDUE = "OVERDUE";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; // 使用包装类型Long

    @Column(nullable = false, length = 50)
    private String contractNumber; // 合同编号

    @Column(nullable = false, length = 100)
    private String customerName; // 客户名称

    @Column(nullable = false)
    private Integer period; // 当前期数

    @Column(nullable = false)
    private Integer totalPeriods; // 总期数

    @Column(nullable = false, precision = 12, scale = 2)
    private BigDecimal principal; // 本金

    @JsonFormat(pattern = "YYYY-MM-dd")
    @Column(nullable = false)
    private Date dueDate; // 到期日

    @Column(nullable = false, length = 20)
    private String status = STATUS_PENDING; // 还款状态

    private Date paidDate; // 实际支付日期

    @Column(length = 500)
    private String remarks; // 备注

    public static RepaymentScheduleInfo mock() {
        RepaymentScheduleInfo r = new RepaymentScheduleInfo();
        r.setContractNumber(String.valueOf(RDU.random(200)));
        r.setCustomerName(RDU.randomCompanyName());
        r.setPeriod(RDU.random(5));
        r.setTotalPeriods(4);
        r.setPrincipal(BigDecimal.valueOf(RDU.randBetween(10000, 9999999)));
        r.setDueDate(RDU.randomDate());

        String[] statuses = {STATUS_PENDING, STATUS_PAID, STATUS_OVERDUE};
        String randomStatus = statuses[RDU.randBetween(0, 2)]; // 随机选一个状态
        r.setStatus(randomStatus);

        if (randomStatus.equals(STATUS_PAID)) {
            r.setPaidDate(randomDateBefore(r.getDueDate()));
        } else {
            r.setPaidDate(null);
        }
        if (RDU.randBetween(0, 1) == 1) {
            r.setRemarks("Mock remarks: " + RDU.random(20)); // 随机生成备注内容
        } else {
            r.setRemarks(null); // 50%概率无备注
        }

        return r;
    }
    private static Date randomDateBefore(Date date) {
        if (date == null) return null;

        long maxDaysBefore = 305; // 最多提前30天
        long oneDayMillis = 24 * 60 * 60 * 1000;

        // 计算日期范围
        long endMillis = date.getTime();
        long startMillis = endMillis - (long)(maxDaysBefore * oneDayMillis);

        // 生成随机日期
        long randomMillis = (long) (Math.random() * (endMillis - startMillis)) + startMillis;
        return new Date(randomMillis);
    }
    public static List<RepaymentScheduleInfo> mock (int n){
        List<RepaymentScheduleInfo> rs = new ArrayList<>();

        for(int i=0;i< n; i++ )
        {
            rs.add(mock());
        }

        return rs;
    }

}
